<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class perusahaan extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_perusahaan');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        $this->load->library("tupload");
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/perusahaan/list.html");
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/perusahaan/index/");
        $config['total_rows'] = $this->m_perusahaan->get_count_all_perusahaan();
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array(($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_perusahaan->get_all_perusahaan_pagination($params));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/perusahaan/add.html");
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input  
        $this->tnotification->set_rules('perusahaan_nama', 'Nama', 'trim|required|max_length[100]'); 

        // cek nama perusahaan    
        if($this->m_perusahaan->is_exist_nama($this->input->post('perusahaan_nama')) != NULL) {
            $this->tnotification->sent_notification("error", 'Nama Perusahaan is not available');
        }else{
            // process 
            if ($this->tnotification->run() !== FALSE) {  
                $filename = '';
                if (!empty($_FILES['perusahaan_logo']['tmp_name'])) { 
                    // last id
                    $id = $this->m_site->get_new_id(); 
                    // upload config
                    $config['upload_path'] = 'resource/perusahaan/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['file_name'] = $id;
                    $this->tupload->initialize($config);
                    // process upload images
                    if ($this->tupload->do_upload_image('perusahaan_logo')) { 
                        $data = $this->tupload->data(); 
                        $filename = $data['file_name'];
                        // notification
                        $this->tnotification->delete_last_field();
                        $this->tnotification->sent_notification("success", "Gambar berhasil diunggah");
                    } else {
                        // jika gagal
                        $this->tnotification->set_error_message($this->tupload->display_errors());
                    }
                } 
                // parameter
                $params = array( 
                    $this->input->post('perusahaan_nama'),
                    $filename,
                    $this->com_user['user_id']
                );
                // insert
                if ($this->m_perusahaan->insert($params)) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                } 
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } 
        // default redirect
        redirect("master/perusahaan/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/perusahaan/edit.html");
        //data
        $result = $this->m_perusahaan->get_perusahaan_by_id($id);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('perusahaan_id', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('perusahaan_nama', 'Nama', 'trim|required|max_length[100]'); 

        // process
        if ($this->tnotification->run() !== FALSE) {
            $filename = '';
            $result = $this->m_perusahaan->get_perusahaan_by_id($this->input->post('perusahaan_id'));
            if (!empty($_FILES['perusahaan_logo']['tmp_name'])) { 
                // last id
                $id = $this->m_site->get_new_id(); 
                // upload config
                $config['upload_path'] = 'resource/perusahaan/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
                $config['file_name'] = $id;
                $this->tupload->initialize($config);
                // process upload images
                if ($this->tupload->do_upload_image('perusahaan_logo')) { 
                    $data = $this->tupload->data(); 
                    $filename = $data['file_name'];
                    unlink('resource/perusahaan/'.$result['perusahaan_logo']);
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Gambar berhasil diunggah");
                } else {
                    // jika gagal
                    $this->tnotification->set_error_message($this->tupload->display_errors());
                }
            }else{ 
                $filename = $result['perusahaan_logo'];
            }
            // parameter
            $params = array(
                $this->input->post('perusahaan_nama'),
                $filename,
                $this->com_user['user_id'],
                $this->input->post('perusahaan_id')
            );
            // insert
            if ($this->m_perusahaan->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/perusahaan/edit/" . $this->input->post('perusahaan_id'));
    }

    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/perusahaan/delete.html");
        //data
        $result = $this->m_perusahaan->get_perusahaan_by_id($id);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('perusahaan_id', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('perusahaan_id')
            );
            // insert
            if ($this->m_perusahaan->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/perusahaan/");
    } 
} 